//
//  HJReportController.m
//  KaiChat
//
//  Created by lx on 2023/11/27.
//

#import "HJReportController.h"
#import "HJNavigationController.h"
#import "QMUITextView.h"
#import "HJReportTypeController.h"
#import "HJMinePresent.h"

@interface HJReportController ()

@property (nonatomic, assign) NSInteger maxLenght;
@property (nonatomic, weak) QMUITextView *detailTextView;
@property (nonatomic, weak) UILabel *detailLengthLabel;
@property (nonatomic, weak) UILabel *typeLabel;
@property (nonatomic, weak) HJStyle1Button *OKButton;
@property (nonatomic, copy) NSString *selectType;
@property (nonatomic, strong) NSArray *typeListDatas;
@property (nonatomic, strong) UIScrollView *mainView;

@end

@implementation HJReportController

- (void)showInController:(UIViewController *)controller {
    HJNavigationController *navController = [[HJNavigationController alloc] initWithRootViewController:self];
    [controller presentViewController:navController animated:YES completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationBar.title = @"Report";
    self.maxLenght = 600;
    
    
    self.mainView = [UIScrollView addToView:self.view];
    self.mainView.frame = CGRectMake(0, KNavBarHight, KScreenWidth, KScreenHeight - KNavBarHight - TabbarSafeBottomMargin - 20 - 55);
    self.mainView.backgroundColor = UIColor.tj_backgroundColor;
    CGSize contentSize = self.mainView.bounds.size;
    
    self.mainView.contentSize = contentSize;
    self.mainView.showsVerticalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        self.mainView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    UIView *contentView = [[UIView alloc] init].addTo(self.mainView);
    contentView.backgroundColor = UIColor.tj_backgroundColor;
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
        make.width.offset(contentSize.width);
        make.height.offset(contentSize.height);
    }];
    
    UIView *typeView = [UIView addToView:contentView];
    typeView.backgroundColor = UIColor.whiteColor;
    [typeView addTapGesturesWithTarget:self action:@selector(tapTypeView)];
    [typeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.offset(0);
        make.height.offset(50);
    }];
    
    UIImageView *arrowImageView = [UIImageView addToView:typeView];
    arrowImageView.image = [UIImage imageNamed:@"cell_arrow"];
    [arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.inset(leftRightMargin);
        make.centerY.offset(0);
    }];
 
    UILabel *textLabel = [UILabel lableWithSuperView:typeView font:UIFontMake(14) color:UIColor.tj_333333Color title:@"Report type *" textAlignment:NSTextAlignmentLeft];
    textLabel.attributedText = [textLabel.text attributedStringWithRangeString:@"*" rangeColor:UIColor.tj_FF5100Color color:UIColor.tj_333333Color];
    [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(leftRightMargin);
        make.centerY.offset(0);
    }];
    
    UILabel *typeLabel = [UILabel lableWithSuperView:typeView font:UIFontMake(14) color:UIColor.tj_868391Color title:@"Please choose" textAlignment:NSTextAlignmentLeft];
    [typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(arrowImageView.mas_left).inset(6);
        make.centerY.offset(0);
    }];
    
    UILabel *typeDetailLabel = [UILabel lableWithSuperView:typeView font:UIFontMake(14) color:UIColor.tj_333333Color title:@"Report details type *" textAlignment:NSTextAlignmentLeft];
    typeDetailLabel.attributedText = [typeDetailLabel.text attributedStringWithRangeString:@"*" rangeColor:UIColor.tj_FF5100Color color:UIColor.tj_333333Color];
    [typeDetailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(textLabel);
        make.top.equalTo(typeView.mas_bottom);
        make.height.offset(40);
    }];
    
    UIView *detailView = [UIView addToView:contentView];
    detailView.addCornerRadius(10);
    detailView.backgroundColor = UIColor.tj_F6F6F6Color;
    [detailView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(typeDetailLabel);
        make.right.inset(leftRightMargin);
        make.top.equalTo(typeDetailLabel.mas_bottom);
        make.height.offset(336);
    }];

    UILabel *detailLengthLabel = [UILabel lableWithSuperView:detailView font:UIFontMake(12) color:UIColor.tj_BEBDCAColor title:nil textAlignment:NSTextAlignmentLeft];
    [detailLengthLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.inset(15);
        make.bottom.inset(10);
    }];
    
    QMUITextView *detailTextView = [QMUITextView addToView:detailView];
    detailTextView.textColor = UIColor.tj_333333Color;
    detailTextView.font = UIFontMake(14);
    detailTextView.placeholderColor = UIColor.tj_placeholderTextColor;
    detailTextView.placeholder = @"Please fill in the report details.";
    detailTextView.backgroundColor = UIColor.tj_F6F6F6Color;
    detailTextView.maximumTextLength = _maxLenght;
    detailTextView.tintColor = UIColor.tj_mainColor;
    [detailTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.top.offset(8);
        make.bottom.equalTo(detailLengthLabel.mas_top).inset(10);
        make.right.inset(10);
    }];

    
    HJStyle2Button *cancelButton = [HJStyle2Button buttonWithSuperView:self.view font:UIFontMediumMake(14) color:nil title:@"Cancel"];
    [cancelButton addTarget:self action:@selector(didClickCancelButton) forControlEvents:UIControlEventTouchUpInside];
    [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.inset(leftRightMargin);
        make.right.equalTo(self.view.mas_centerX).inset(12);
        make.height.offset(55);
        make.bottom.inset(10 + TabbarSafeBottomMargin);
    }];
    
    HJStyle1Button *OKButton = [HJStyle1Button buttonWithSuperView:self.view font:UIFontMediumMake(14) color:nil title:@"OK"];
    OKButton.enabled = NO;
    [OKButton addTarget:self action:@selector(didClickOKButton) forControlEvents:UIControlEventTouchUpInside];
    [OKButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(cancelButton);
        make.right.inset(leftRightMargin);
        make.left.equalTo(self.view.mas_centerX).inset(12);
        make.bottom.equalTo(cancelButton);
    }];
    
    _detailTextView = detailTextView;
    _detailLengthLabel = detailLengthLabel;
    _typeLabel = typeLabel;
    _OKButton = OKButton;
    
    [self updateDetailLengthLabelText];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextChanged:) name:UITextViewTextDidChangeNotification object:detailTextView];
    [self loadData];
}

- (void)tapTypeView {
    HJReportTypeController *reportTypeController = [[HJReportTypeController alloc] init];
    reportTypeController.type = self.selectType;
    reportTypeController.typeListDatas = self.typeListDatas;
    TJWeakify(self)
    [reportTypeController setSelectCompleteBlock:^(NSString * _Nonnull type) {
        weakself.typeLabel.text = type;
        weakself.selectType = type;
        [weakself updateDetailLengthLabelText];
    }];
    [self presentViewController:reportTypeController animated:NO completion:nil];
}

- (void)loadData {
    [HJMinePresent loadReportTypeListDataComplete:^(NSArray * _Nullable datas) {
        self.typeListDatas = datas;
    }];
}

- (void)didClickCancelButton {
    [self popViewController];
}

- (void)didClickOKButton {
    if (isEmptyString(_detailTextView.text)) {
        [HJHUD showErrorMessage:@"The details can not be empty"];
        return;
    }
    [HJHUD showLoadingInView:self.view];
    [HJMinePresent reportFridend:self.userId type:_selectType detail:_detailTextView.text complete:^(NSError * _Nullable error) {
        if (!error) {
            [HJHUD showSucceedMessage:@"Report successful"];
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    }];
}

- (void)handleTextChanged:(NSNotification *)not {
   if (not.object != _detailTextView) return;
   
   [self updateDetailLengthLabelText];
}

- (void)updateDetailLengthLabelText {
    
    _OKButton.enabled = _selectType && _detailTextView.text.length;

   if ( _detailTextView.text.length > 0) {
       _detailLengthLabel.attributedText = [[NSString stringWithFormat:@"%ld/%ld", _detailTextView.text.length, _maxLenght] attributedStringWithRangeString:[NSString stringWithFormat:@"%ld", _detailTextView.text.length] rangeColor:UIColor.tj_333333Color color:UIColor.tj_868391Color];
   } else {
       _detailLengthLabel.attributedText = [[NSString stringWithFormat:@"%ld/%ld", _detailTextView.text.length, _maxLenght] attributedStringWithRangeString:[NSString stringWithFormat:@"%ld", _detailTextView.text.length] rangeColor:UIColor.tj_868391Color color:UIColor.tj_868391Color];
   }

}

- (NSString *)backButtonImageName {
    return @"nav_close";
}

 
- (UIModalPresentationStyle)modalPresentationStyle {
    return UIModalPresentationFullScreen;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:_detailTextView];
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [IQKeyboardManager sharedManager].toolbarTintColor = UIColor.tj_mainColor;
    [IQKeyboardManager sharedManager].toolbarDoneBarButtonItemText = @"Done";
    [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
}

 

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [IQKeyboardManager sharedManager].toolbarDoneBarButtonItemText = nil;
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
}
 

@end
